package com.springpad;

import com.database.NotebookAdapter;
import com.model.Notebook;
import firstdroid.tutorial.gridview.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.EditText;
import android.widget.GridView;

import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MyGridView extends Activity {
	GridView MyGrid;
	static int len;
	AlertDialog.Builder alert;
	private Context ctx;
	NotebookAdapter na;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ctx = this;
		na = new NotebookAdapter(getApplicationContext());

		MyGrid = (GridView) findViewById(R.id.MyGrid);
		MyGrid.setAdapter(new ImageAdapter(this));

		MyGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				if (position == (len - 1)) {

					alert = new AlertDialog.Builder(ctx);
					alert.setTitle("Add a New Notebook");
					final EditText name = new EditText(MyGridView.this);
					alert.setView(name);
					alert.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									String notename = name.getText().toString();
									 createNewNotebook(notename,0);
									 startActivity(new Intent(MyGridView.this,Menu.class));
								}
							});
					alert.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {

								}
							});
					alert.create().show();

					// Toast.makeText(MyGridView.this, "" + position+"hiiii",
					// Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MyGridView.this, "" + position,
							Toast.LENGTH_SHORT).show();
				}
			}

		});

	}

	private void createNewNotebook(String name, int item) {
		Notebook n = new Notebook();
		n.setName(name);
		n.setItems(item);
		na.open();
		na.createNoteBook(n);
		na.close();
	}

	public class ImageAdapter extends BaseAdapter {
		Context MyContext;

		public ImageAdapter(Context _MyContext) {
			MyContext = _MyContext;
		}

		@Override
		public int getCount() {
			len = 4;
			return len;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View MyView = convertView;

			if (convertView == null) {
				if (position < (getCount() - 1)) {
					LayoutInflater li = getLayoutInflater();
					MyView = li.inflate(R.layout.grid_item, null);

					TextView tv = (TextView) MyView
							.findViewById(R.id.textView1);
					tv.setText("Item " + position);
				} else {
					LayoutInflater li = getLayoutInflater();
					MyView = li.inflate(R.layout.additem, null);

				}

			}

			return MyView;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
	}
}